// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.patent.service.impl;

import com.alibaba.fastjson.JSON;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.enums.ProjectStageEnum;
import com.liuxinlong.modules.dao.PatentProposalDao;
import com.liuxinlong.modules.dao.PatentProposalHistoryDao;
import com.liuxinlong.modules.dao.PatentRiskDao;
import com.liuxinlong.modules.dao.PatentRiskHistoryDao;
import com.liuxinlong.modules.dao.PatentRiskProcessDao;
import com.liuxinlong.modules.entity.PatentProposal;
import com.liuxinlong.modules.entity.PatentProposalHistory;
import com.liuxinlong.modules.entity.PatentRisk;
import com.liuxinlong.modules.entity.PatentRiskHistory;
import com.liuxinlong.modules.entity.PatentRiskProcess;
import com.liuxinlong.modules.patent.service.PatentRiskProcessService;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专利风险管控过程实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-05-21
 */
@Service
@Slf4j
public class PatentRiskProcessServiceImpl implements PatentRiskProcessService {

    @Autowired
    private PatentRiskProcessDao patentRiskProcessDao;

    @Autowired
    private PatentRiskHistoryDao patentRiskHistoryDao;

    @Autowired
    private PatentProposalHistoryDao patentProposalHistoryDao;

    @Autowired
    private PatentProposalDao patentProposalDao;

    @Autowired
    private PatentRiskDao patentRiskDao;

    @Override
    public List<Map<String, Object>> pagePatentRiskProcessInfo(Map<String, Object> queryParam) {
        List<PatentRiskProcess> originList = patentRiskProcessDao.pagePatentRiskProcessList(queryParam);
        return completeResult(originList, (int) queryParam.get("startNum"));
    }

    @Override
    public int countPatentRiskProcess(Map<String, Object> queryParam) {
        return patentRiskProcessDao.getPatentRiskProcessCount(queryParam);
    }

    @Transactional
    @Override
    public String createNewProcess(String businessId,String expand,String currentTime) {
        // 管控阶段
        PatentRiskProcess currentSortProcess = patentRiskProcessDao.getCurrentSort(businessId);
        PatentRiskProcess patentRiskProcess = new PatentRiskProcess();
        String processId = SnowFlake.nextIdStr();
        patentRiskProcess.setId(processId);
        patentRiskProcess.setProjectId(businessId);
        patentRiskProcess.setSortNum(ObjectUtils.isEmpty(currentSortProcess) ? 1 : currentSortProcess.getSortNum() + 1);
        Map<String, Object> applyData = JSON.parseObject(expand);
        patentRiskProcess.setRiskStage((Integer) applyData.get("stage"));
        patentRiskProcess.setStartTime(currentTime);
        patentRiskProcess.setStatus(1);
        patentRiskProcessDao.insert(patentRiskProcess);
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("projectId",businessId);
        queryParam.put("startNum",0);
        queryParam.put("pageSize",10000);
        // 风险点数据备份
        List<PatentRisk> patentRiskList = patentRiskDao.pagePatentRiskList(queryParam);
        List<PatentRiskHistory> patentRiskHistoryList = new ArrayList<>(patentRiskList.size());
        for (PatentRisk risk: patentRiskList) {
            PatentRiskHistory patentRiskHistory = new PatentRiskHistory(risk);
            patentRiskHistory.setProcessId(processId);
            patentRiskHistory.setId(SnowFlake.nextIdStr());
            patentRiskHistoryList.add(patentRiskHistory);
        }
        if (!ObjectUtils.isEmpty(patentRiskHistoryList)) {
            patentRiskHistoryDao.insertBatch(patentRiskHistoryList);
        }
        // 风险专利数据备份
        List<PatentProposal> patentProposalList = patentProposalDao.pagePatentProposalList(queryParam);
        List<PatentProposalHistory> patentProposalHistoryList = new ArrayList<>(patentProposalList.size());
        for (PatentProposal proposal: patentProposalList) {
            PatentProposalHistory patentProposalHistory = new PatentProposalHistory(proposal);
            patentProposalHistory.setProcessId(processId);
            patentProposalHistory.setId(SnowFlake.nextIdStr());
            patentProposalHistoryList.add(patentProposalHistory);
        }
        if (!ObjectUtils.isEmpty(patentProposalHistoryList)) {
            patentProposalHistoryDao.insertBatch(patentProposalHistoryList);
        }
        return processId;
    }

    /**
     * 补全数据
     *
     * @param originList 原始数据
     * @param startNum   起始值
     * @return 补全后数据
     */
    private List<Map<String, Object>> completeResult(List<PatentRiskProcess> originList, int startNum) {
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        int sort = startNum + 1;
        for (PatentRiskProcess process : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(process);
            map.put("sort", sort);
            map.put("statusName", process.getStatus() == 1 ? "进行中" : process.getStatus() == 2 ? "已完成" :
                    process.getStatus() == 3 ? "废止" : "未知");
//            map.put("stageName", ProjectStageEnum.getNameByValue(process.getRiskStage()));
//            map.put("title", String.valueOf(process.getSortNum()));
//            map.put("description", ProjectStageEnum.getNameByValue(process.getRiskStage()) + "阶段");
//            map.put("subTitle", process.getStartTime());
            map.put("title", ProjectStageEnum.getNameByValue(process.getRiskStage()) + "阶段");
            map.put("description", process.getStartTime());
            map.put("subTitle", "评估结论： " + (process.getStatus() == 1 ? "评估进行中" : "已完成评估，" + process.getConclusion())
            + " 评估人: " + process.getAssessor());

//            map.put("conclusion", "评估结论： " + (process.getStatus() == 1 ? "评估进行中" : "已完成评估，" + process.getConclusion())
//                    + " 评估人: " + process.getAssessor());
//            map.put("assessor", " 评估人: " + process.getAssessor());
            resultList.add(map);
            sort++;
        }
        return resultList;
    }

}
